﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using AddressManager.Business.Web.EntityFramework;
using System.Collections.Generic;

namespace AddressManager.Business.Web.WebService
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AddressManagerWebService
    {
        [OperationContract]
        public void DoWork()
        {
            return;
        }

        #region Adding

        [OperationContract]
        public void AddNewContact(Contact c)
        {
            var ctx = new AddressManagerEntities();

            ctx.AddToContacts(c);

            ctx.SaveChanges();
        }

        [OperationContract]
        public void AddNewAddress(Address a)
        {
            var ctx = new AddressManagerEntities();

            ctx.AddToAddresses(a);

            ctx.SaveChanges();
        }

        [OperationContract]
        public void AddNewPhoneNumber(PhoneNumber p)
        {
            var ctx = new AddressManagerEntities();

            ctx.AddToPhoneNumbers(p);

            ctx.SaveChanges();
        }

        [OperationContract]
        public void AddNewPhoneNumberType(PhoneNumberType pType)
        {
            var ctx = new AddressManagerEntities();

            ctx.AddToPhoneNumberTypes(pType);

            ctx.SaveChanges();
        }

        #endregion

        #region Deletion

        [OperationContract]
        public void DeleteContact(Contact c)
        {
            var ctx = new AddressManagerEntities();

            ctx.DeleteObject(ctx.Contacts.SingleOrDefault(ct => ct.Id == c.Id));

            ctx.SaveChanges();
        }

        [OperationContract]
        public void DeletePhoneNumber(PhoneNumber p)
        {
            var ctx = new AddressManagerEntities();

            ctx.DeleteObject(ctx.PhoneNumbers.SingleOrDefault(pn => pn.Id == p.Id));

            ctx.SaveChanges();
        }

        [OperationContract]
        public void DeletePhoneNumberType(PhoneNumberType t)
        {
            var ctx = new AddressManagerEntities();

            var isUsed = ctx.PhoneNumbers.Where(p => p.PhoneNumberType.Id == t.Id).Any();

            if (isUsed)
            {
                throw new Exception(string.Format("PhoneNumberType {0} is still in use.", t.Title));
            }

            ctx.DeleteObject(ctx.PhoneNumberTypes.SingleOrDefault(pnt => pnt.Id == t.Id));

            ctx.SaveChanges();
        }

        #endregion

        #region Changing

        [OperationContract]
        public void ChangeContact(Contact c)
        {
            var ctx = new AddressManagerEntities();

            var contact2Change = ctx.Contacts.SingleOrDefault(ct => ct.Id == c.Id);

            if (contact2Change != null)
            {
                contact2Change.FirstName = c.FirstName;
                contact2Change.LastName = c.LastName;
                contact2Change.Birthday = c.Birthday;
                contact2Change.Email = c.Email;
                contact2Change.Msn = c.Msn;
                contact2Change.Icq = c.Icq;
                contact2Change.Address = c.Address;
                contact2Change.PhoneNumber = c.PhoneNumber;

                ctx.SaveChanges();
            }
        }

        [OperationContract]
        public void ChangeAddress(Address a)
        {
            var ctx = new AddressManagerEntities();

            var address2Change = ctx.Addresses.SingleOrDefault(add => add.Id == a.Id);

            if (address2Change != null)
            {
                address2Change.Street = a.Street;
                address2Change.Number = a.Number;
                address2Change.PostalCode = a.PostalCode;
                address2Change.City = a.City;
                address2Change.LocationX = a.LocationX;
                address2Change.LocationY = a.LocationY;
                address2Change.Contact = a.Contact;

                ctx.SaveChanges();
            }
        }

        [OperationContract]
        public void ChangePhoneNumber(PhoneNumber pn)
        {
            var ctx = new AddressManagerEntities();

            var number2Change = ctx.PhoneNumbers.SingleOrDefault(p => p.Id == pn.Id);

            if (number2Change != null)
            {
                number2Change.Provider = pn.Provider;
                number2Change.Prefix = pn.Prefix;
                number2Change.Number = pn.Number;
                number2Change.PhoneNumberType = pn.PhoneNumberType;
                number2Change.Contact = pn.Contact;

                ctx.SaveChanges();
            }
        }

        [OperationContract]
        public void ChangePhoneNumberType(PhoneNumberType t)
        {
            var ctx = new AddressManagerEntities();

            var type2Change = ctx.PhoneNumberTypes.SingleOrDefault(pnt => pnt.Id == t.Id);

            if (type2Change != null)
            {
                type2Change.Title = t.Title;

                ctx.SaveChanges();
            }
        }

        #endregion

        #region Entity Getter

        #region Contacts

        [OperationContract]
        public Contact GetContactById(int id)
        {
            var ctx = new AddressManagerEntities();

            return ctx.Contacts.SingleOrDefault(c => c.Id == id);
        }

        [OperationContract]
        public bool ContactExists(string firstName, string lastName)
        {
            var ctx = new AddressManagerEntities();

            return ctx.Contacts.Any(c => c.FirstName == firstName && c.LastName == lastName);
        }

        [OperationContract]
        public IEnumerable<Contact> GetAllContacts()
        {
            var ctx = new AddressManagerEntities();

            return ctx.Contacts;
        }

        #endregion

        #region Addresses

        [OperationContract]
        public Address GetAddressById(int id)
        {
            var ctx = new AddressManagerEntities();

            return ctx.Addresses.SingleOrDefault(a => a.Id == id);
        }

        [OperationContract]
        public IEnumerable<Address> GetAllAddresses()
        {
            var ctx = new AddressManagerEntities();

            return ctx.Addresses;
        }

        #endregion

        #region PhoneNumberTypes

        [OperationContract]
        public IEnumerable<PhoneNumberType> GetAllPhoneNumberTypes()
        {
            var ctx = new AddressManagerEntities();

            return ctx.PhoneNumberTypes;
        }

        #endregion

        #endregion
    }
}